package basic;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author： pang-yun
 * @date： 2021-08-28 15:38
 * 功能目的 ：用于
 */

public class Buffer {
    @Test
    public void buffer() {
        ByteBuffer b = ByteBuffer.allocate(10);
        b.put(new byte[]{'1', '2', '3', '4'});
        b.flip();
        byte b1 = b.get();
        System.out.println((char) b1);
        System.out.println((char) b.get());
        System.out.println((char) b.get());
        b.rewind();
        System.out.println((char) b.get());
        System.out.println("开始标记-----------------mark");
        b.mark();
        System.out.println(b.get());
        System.out.println(b.get());
        b.reset();
        System.out.println(b.get());

    }

    @Test
    public void bufferString() {
        ByteBuffer allocate = ByteBuffer.allocate(16);
        allocate.put("herman".getBytes());
        allocate.flip();
        while (allocate.hasRemaining()) {
            System.out.print((char) allocate.get());
        }

        System.out.println();
        //--------------------
        ByteBuffer encode = StandardCharsets.UTF_8.encode("PangChaoJie");
        while (encode.hasRemaining()){
            System.out.print((char) encode.get());
        }

        System.out.println();

        ByteBuffer wrap = ByteBuffer.wrap("hero".getBytes());
        while (wrap.hasRemaining()){
            System.out.print((char) wrap.get());
        }
        //-------------
    }
}
